{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Pygrid - MNIST Training (Grid-Admin)\n",
    "In this notebook, we'll be showing how to perform a Remote training with MNIST dataset using PyGrid/PySyft 0.3.0.\n",
    "\n",
    "**Author**: Ionésio Junior **Github**: [@IonesioJunior](https://github.com/IonesioJunior)    \n",
    "**Author**: Madhava Jay **Github**: [@madhavajay](https://github.com/madhavajay)    \n",
    "**Author**: Tudor Cebere **Github**: [@tudorcebere](https://github.com/tudorcebere)    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from syft.grid.grid_client import connect\n",
    "from syft.grid.connections.http_connection import HTTPConnection\n",
    "from syft.core.node.domain.client import DomainClient\n",
    "\n",
    "import syft as sy\n",
    "import torch as th\n",
    "import torchvision\n",
    "\n",
    "sy.VERBOSE = False"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Connect/Authenticate with the Domain server (Using an admin account)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "client = connect(\n",
    "    url=\"http://localhost:5000\", # Domain Address\n",
    "    credentials= {\"email\": \"admin@email.com\", \"password\": \"pwd123\"}, # Admin role\n",
    "    conn_type= HTTPConnection, # HTTP Connection Protocol\n",
    "    client_type=DomainClient) # Domain Client type"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 3 - Retrieve some domain setting attributes"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "# lets get some references to our Domain torch and torchvision\n",
    "torch = client.torch\n",
    "torchvision = client.torchvision\n",
    "\n",
    "# these are the same as the original mnist example\n",
    "transforms = torchvision.transforms\n",
    "datasets = torchvision.datasets\n",
    "nn = torch.nn\n",
    "F = torch.nn.functional\n",
    "optim = torch.optim\n",
    "StepLR = torch.optim.lr_scheduler.StepLR"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Create automatic data request handlers"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "accept_handler = {\n",
    "    \"request_name\": \"cuda_is_available\",\n",
    "    \"timeout_secs\": -1,\n",
    "    \"action\": \"accept\",\n",
    "}\n",
    "client.requests.add_handler(accept_handler)\n",
    "accept_handler = {\n",
    "     \"request_name\": \"loss\",\n",
    "     \"timeout_secs\": -1,\n",
    "     \"action\": \"accept\",\n",
    " }\n",
    "client.requests.add_handler(accept_handler)\n",
    "accept_handler = {\n",
    "    \"request_name\": \"train_size\",\n",
    "    \"timeout_secs\": -1,\n",
    "    \"action\": \"accept\",\n",
    "    \"print_local\": True,\n",
    "    \"log_local\": True\n",
    "}\n",
    "client.requests.add_handler(accept_handler)\n",
    "accept_handler = {\n",
    "    \"request_name\": \"inference\",\n",
    "    \"timeout_secs\": -1,\n",
    "    \"action\": \"accept\",\n",
    "    \"print_local\": True,\n",
    "    \"log_local\": True\n",
    "}\n",
    "client.requests.add_handler(accept_handler)\n",
    "accept_handler = {\n",
    "    \"request_name\": \"model_download\",\n",
    "    \"timeout_secs\": -1,\n",
    "    \"action\": \"accept\",\n",
    "}\n",
    "client.requests.add_handler(accept_handler)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
